﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace One_day.Areas.admin.Controllers
{
    public class CategoryController : Controller
    {
        // GET: admin/Category
        /// <summary>
        /// 初始进入界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            List<One_day.Models.T_Shop_Category> lst = db.T_Shop_Category.ToList();
            ViewBag.lst = lst;
            return View();
        }
        /// <summary>
        /// 用于增加种类
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {

            return View();
        }
        public ActionResult AddSave(String CategoryName,HttpPostedFileBase PicUrl)
        {
            string filename = "";

            if (PicUrl == null || String.IsNullOrEmpty(PicUrl.FileName) || PicUrl.ContentLength == 0)
            {

            }
            else
            {
                filename = System.IO.Path.GetFileName(PicUrl.FileName);
                string virtualPath = String.Format("~/Upload/{0}", filename);
                string path = Server.MapPath(virtualPath);
                PicUrl.SaveAs(path);
            }
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Shop_Category cate = new Models.T_Shop_Category();
            cate.CatagoryName = CategoryName;
            cate.PicUrl = filename;
            db.T_Shop_Category.Add(cate);
            db.SaveChanges();
            return Redirect("/admin/category/index");
        }
        public JsonResult Delete(int id)
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Shop_Category category = db.T_Shop_Category.Find(id);
            db.T_Shop_Category.Remove(category);
            int result = 0;
            try//若所要删除的信息被其他表所调用，便无法删除
            {
                result = db.SaveChanges();
            }
            catch
            {
                return Json(new { code = 500, message = "删除失败" });
            }

            if (result > 0)
            {
                return Json(new { code = 200, message = "删除成功" });
            }
            else
                return Json(new { code = 500, message = "删除失败" });
        }

        public ActionResult Update(int id)//id为需要修改的T_Shop_Category 的id号
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Shop_Category cate = db.T_Shop_Category.Single(m => m.Id == id);//获取对应id的一整行信息，并传递
            ViewBag.cate = cate;
            return View();
        }

        public ActionResult UpdateSave(String CategoryName,HttpPostedFileBase PicUrl,int id)
        {
            string filename = "";
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Shop_Category cate = db.T_Shop_Category.Single(m => m.Id == id);
            if (PicUrl == null || String.IsNullOrEmpty(PicUrl.FileName) || PicUrl.ContentLength == 0)
            {
                cate.CatagoryName = CategoryName;
                db.SaveChanges();
            }
            else
            {
                filename = System.IO.Path.GetFileName(PicUrl.FileName);
                string virtualPath = String.Format("~/Upload/{0}", filename);
                string path = Server.MapPath(virtualPath);
                PicUrl.SaveAs(path);
                cate.CatagoryName = CategoryName;
                cate.PicUrl = filename;
                db.SaveChanges();
            }
            return Redirect("/admin/category/index");
        }
    }
}